package cn.cheegon.blog.controller;

import cn.cheegon.blog.common.api.CommonPage;
import cn.cheegon.blog.common.api.CommonResult;
import cn.cheegon.blog.entity.dto.CmsBlogDto;
import cn.cheegon.blog.entity.dto.PmsTypeDto;
import cn.cheegon.blog.entity.req.PmsTypeReq;
import cn.cheegon.blog.model.CmsBlog;
import cn.cheegon.blog.model.PmsType;
import cn.cheegon.blog.service.PmsTypeService;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 类型表 前端控制器
 * </p>
 *
 * @author cheegon
 * @since 2024-06-21
 */
@RestController
@RequestMapping("/admin/type/*")
public class AdminTypeController extends AdminController{
    @Autowired
    private PmsTypeService pmsTypeService;

    @Operation(summary = "获取类型里列表", description = "")
    @GetMapping("list")
    public CommonResult<CommonPage<PmsTypeDto>> getTypeList(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(name = "pageSize", defaultValue = "20") Integer pageSize) {
        CommonPage<PmsTypeDto> list = pmsTypeService.getList(pageNum, pageSize);
        return CommonResult.success(list);
    }

    @GetMapping("topList")
    public CommonResult<List<PmsTypeDto>> getTopTypeList(@RequestParam(name = "size", defaultValue = "6") Integer size) {
        List<PmsTypeDto> list = pmsTypeService.getTopList(size);
        return CommonResult.success(list);
    }

    @GetMapping("allList")
    public CommonResult<List<PmsTypeDto>> getAllTypeList() {
        return CommonResult.success(pmsTypeService.getAllList());
    }

    @PostMapping("edit")
    public CommonResult edit(@RequestBody PmsTypeReq req) {
        checkAdmin();
        pmsTypeService.edit(BeanUtil.toBean(req, PmsType.class));
        return CommonResult.success();
    }

    @PostMapping("add")
    public CommonResult add(@RequestBody PmsTypeReq req) {
        checkAdmin();
        pmsTypeService.add(BeanUtil.toBean(req, PmsType.class));
        return CommonResult.success();
    }
    @GetMapping("del/{id}")
    public CommonResult del(@PathVariable Long id) {
        checkAdmin();
        pmsTypeService.del(id);
        return CommonResult.success();
    }
}
